import android.media.MediaCodec;
import android.media.MediaFormat;
import android.util.Log;

import java.nio.ByteBuffer;


public class AudioCenter {

    public static final int REVERB_DEFAULT = 0;
    public static final int REVERB_KTV = REVERB_DEFAULT + 1;
    public static final int REVERB_ROOM = REVERB_KTV + 1;
    public static final int REVERB_HALL = REVERB_ROOM + 1;


    public interface Listener{
        void onAudioPlayStart(int current, int duration);
        void onAudioPlayPause(int current, int duration);
        void onAudioPlayProcess(byte[] data, int channels, int sampleRate, int current, int duration);
        void onAudioPlayEnd(int errCode);

        /**
         * 获取采集PCM，当调用{@link AudioCenter#startMIC(int)}后通过该接口获取数据
         * @param data
         * @param timestamp
         */
        void onCapturePCM(byte[] data, long timestamp, int channels, int sampleRate);
        void onPCMProcessed(byte[] data, long ts, int channels, int sampleRate);
    }

    public void setListener(Listener listener){
        mListener = listener;
        mAudioNative.setListener(mListener);
    }

    public AudioCenter(){
        mAudioNative = new AudioNative();
    }

    public synchronized int play(String path){
        return mAudioNative.open(path);
    }

    private AudioListener mMicListener = new AudioListener() {
        @Override
        public void onPCM(byte[] data, long timestamp, int channels, int sampleRate) {
            if(mListener != null){
                mListener.onCapturePCM(data, timestamp, channels, sampleRate);
            }
        }

        @Override
        public void onErr(int errCode) {

        }

        @Override
        public void onEncFormat(MediaFormat format) {

        }

        @Override
        public void onEncData(ByteBuffer byteBuf, MediaCodec.BufferInfo bufferInfo) {

        }
    };

    public synchronized boolean startMIC(int channels){
        if(mAudioCapture == null){
            mAudioCapture = new AudioCapturer();
        }
        return mAudioCapture.start(DDAudioDef.SAMPLE_RATE_44100, channels == 1 ? DDAudioDef.CHANNELS_MONO : DDAudioDef.CHANNELS_STEREO, DDAudioDef.BITS_PER_PCM_UNIT_16, false, mMicListener);
    }

    public void processPCM(byte[] data, long ts, int channels, int sampleRate){
        mAudioNative.processPCM(data, ts, channels, sampleRate);
    }

    public synchronized void stop(){
        mAudioNative.stop();
        if(mAudioCapture != null){
            mAudioCapture.stop();
        }
    }

    /**
     * 底层库目前仅适配44100，即AudioCenter输出采样率统一为44100
     * @param inputPath
     * @param outputPath
     * @return
     */

    public synchronized void setSpeed(float speed, boolean isGlobal){
        mAudioNative.setSpeed(speed, isGlobal);
    }

    public synchronized void setPitch(float pitch, boolean isGlobal){
        mAudioNative.setPitch(pitch, isGlobal);
    }

    public synchronized void setDistance(float distance, boolean isGlobal){
        mAudioNative.setDistance(distance, isGlobal);
    }

    public synchronized void seek(int ms){
        mAudioNative.seek(ms);
    }

    public synchronized void pause(){
        mAudioNative.pause();
    }

    public synchronized void resume(){
        mAudioNative.resume();
    }

    public synchronized void setReverb(int type){
        mAudioNative.setReverb(type);
    }

//    enum
//    {
//        DAMPINGFREQ=0,
//        DENSITY,
//        BANDWIDTHFREQ,
//        DECAY,
//        PREDELAY,
//        SIZE,
//        GAIN,
//        MIX,
//        EARLYMIX,
//        NUM_PARAMS
//    };
    public synchronized void setReverbValue(int idx, float value){
        mAudioNative.setReverbValue(idx, value);
    }

//                "重低音",1
//                "摇滚",2
//                "流行",3
//                "古典",4
//                "爵士",5
//                "乡村",6
//                "蓝调",7
//                "舞曲",8
    public synchronized int[] setEqualizerType(int type){
        return mAudioNative.setEqualizerType(type);
    }

    public synchronized void setEqualizerValue(int band, int value){
        mAudioNative.setEqualizerValue(band, value);
    }

    private AudioNative                     mAudioNative;
    private AudioCapturer                   mAudioCapture;
    private Listener                        mListener;

}
